/*
 * Copyright 2010 Kevin Bourrillion.
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */

package kevinb9n.tobago;

import com.google.common.base.Strings;

public enum Board {
  // rows go downward to y=0.
  // columns from x=0 to x=7, space separated.
  // formatted weirdly so you can visually compare them to the physical boards

  A( "          M M   ",
      "    B J J M M  ",
       "S B J J J M M ",
        "S J L L J M  ",
         "M L L J J M ",
          "M L R R    ",
           "M M S S   "),


  B(  "    B B B J J  ",
       "  B S S J J   ",
        "M S S J R R  ",
         "M S L R S B ",
          "M L L S S B",
           "L L L S S "),

  C( "        B M     ",
      "  R B   B M M J",
       "  R B B M M J ",
        "S R R L M J B",
         "S S L L J B ",
          "S J J J B  ",
           "J J J M M "),

  a( "        B B M   ",
      "    B J J J B  ",
       "      B J J B ",
        "S B B J      ",
         "S M M M R   ",
          "S L R R    ",
           "L L R R   "),

  b("          J J    ",
     "        J J     ",
      "  S   M J      ",
       "S S M J   B   ",
        "S M J R S B  ",
         "M M R S S B ",
          "L L S      ",
           "L L S S   "),

  c( "        M       ",
      "      B     S  ",
       "    M B S S   ",
        "S M M L B B  ",
         "S M L B     ",
          "S S R B B  ",
           "M M R R R "),
  ;

  private static final String OCEAN_ROW_STRING = "                    ";

  private final BoardHex[][] hexes;
  static final int MAX = 10;

  private Board(String... map) {
    hexes = new BoardHex[MAX][]; // 9th row and column are automatically all-ocean
    for (int y = 0; y < MAX; y++) {
      hexes[y] = new BoardHex[MAX];

      // auto-pad with ocean hexes
      String rowString = (y >= map.length)
          ? OCEAN_ROW_STRING
          : Strings.padEnd(map[map.length - y - 1], 2 * MAX, ' ');

      for (int x = 0; x < MAX; x++) {
        char c = rowString.charAt(x * 2); // skip spaces
        Terrain t = Terrain.forCharacter(c);
        BoardHex hex = new BoardHex(this, x, y, t);
        hexes[y][x] = hex;
      }
    }
  }

  public BoardHex hexAt(int x, int y) {
    return hexes[y][x];
  }
}
